package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.AdminCilentMapper;
import cn.scxy.bookcity.pojo.vo.ActivityListVO;
import cn.scxy.bookcity.pojo.vo.GoodsListItemVO;
import cn.scxy.bookcity.pojo.vo.HotSearchListVO;
import cn.scxy.bookcity.service.IAdminCilentService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 张正荣
 */
@Service
@Slf4j
public class AdminCilentServiceImpl implements IAdminCilentService {
    @Autowired
    AdminCilentMapper mapper;
    @Override
    public List<HotSearchListVO> selectHotList() {
        List<HotSearchListVO> list=mapper.selectHotList();
        return list;
    }
    @Override
    public void setEnable(Long id) {
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Long id) {
        updateEnableById(id, 0);
    }


    private void updateEnableById(Long id, Integer enable) {
        String[] tips = {"下架", "上架"};
        log.debug("开始处理【{}类别】的业务，参数：{}", tips[enable], id);
        // 调用Mapper对象的getDetailsById()方法执行查询
        HotSearchListVO goodsById = mapper.getHotById(id);
        // 判断查询结果是否为null
        if (goodsById == null) {
            String message = tips[enable] + "商品失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 判断查询结果中的enable是否为1
        if (goodsById.getEnable().equals(enable)) {
            String message = tips[enable] + "商品失败，当前类别已经处于" + tips[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 准备执行更新
        HotSearchListVO goods = new HotSearchListVO();
        goods.setId(id);
        goods.setEnable(enable);
        int rows = mapper.update(goods);
        if (rows != 1) {
            String message = tips[enable] + "类别失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public void deleteHot(Long id) {
        mapper.deleteHot(id);
    }

    @Override
    public void insetHot(HotSearchListVO hotSearchListVO) {
        mapper.insetHot(hotSearchListVO);
    }

    @Override
    public List<ActivityListVO> selectActivitylist() {
        List<ActivityListVO> list=  mapper.selectActivitylist();
        return list;
    }
    @Override
    public void setEnable1(Long id) {
        updateEnableById1(id, 1l);
    }

    @Override
    public void setDisable1(Long id) {
        updateEnableById1(id, 0l);
    }

    @Override
    public List<ActivityListVO> aschotlist() {
        List<ActivityListVO> list=mapper.aschotlist();
        return list;
    }

    @Override
    public List<ActivityListVO> deshotlist() {
        List<ActivityListVO> list=mapper.deshotlist();
        return list;
    }

    @Override
    public List<ActivityListVO> selectLikehot(String name) {
        List<ActivityListVO> activityListVO=    mapper.selectLikehot(name);
        return activityListVO;
    }


    private void updateEnableById1(Long id, Long enable) {
        String[] tips = {"下架", "上架"};
        log.debug("开始处理【{}类别】的业务，参数：{}", id);
        // 调用Mapper对象的getDetailsById()方法执行查询
        ActivityListVO goodsById = mapper.getActivityById(id);
        // 判断查询结果是否为null
        if (goodsById == null) {
            String message ="修改失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 判断查询结果中的enable是否为1
        if (goodsById.getState().equals(enable)) {
            String message ="修改失败，当前类别已经处于 此状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 准备执行更新
        ActivityListVO goods = new ActivityListVO();
        goods.setId(id);
        goods.setState(enable);
        int rows = mapper.updateActivity(goods);
        if(enable==1l){
            ActivityListVO goods1 = new ActivityListVO();
            goods1.setId(id);
            goods1.setState(0l);
            mapper.updateActivitys(goods1);
        }
        if (rows != 1) {
            String message ="类别失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

}
